//-----------------------------------------------------------------------------
// File: UiFactoryDescriptor.cs
//
// Copyright (c) Dmitry Shuklin. All rights reserved.
//-----------------------------------------------------------------------------
using System;

namespace ObjectiveDraw.Platform.Reflection
{
	/// <summary>
	/// Summary description for UiFactoryDescriptor.
	/// </summary>
	public class UiFactoryDescriptor : Cerebrum.Integrator.GenericComponent, Cerebrum.Windows.Forms.IUiControlFactory
	{
		public UiFactoryDescriptor()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public ObjectiveDraw.Platform.Application DrawerApplication
		{
			get
			{
				return this.DomainContext.Properties[ObjectiveDraw.Platform.Application.m_ApplicationProperty] as ObjectiveDraw.Platform.Application;
			}
		}

		#region IUiControlFactory Members

		public Cerebrum.Windows.Forms.IUiControl CreateControl(object descriptor)
		{
			ObjectiveDraw.Platform.Application _app = this.DrawerApplication;
			Cerebrum.Direct3D.Forms.TabHeader _ToolBoxHeader = _app.m_ToolBoxHeader;
			Cerebrum.Windows.Forms.Reflection.UiBandItemDescriptor ud = (Cerebrum.Windows.Forms.Reflection.UiBandItemDescriptor)descriptor;

			if(_ToolBoxHeader == null)
			{
				/*m_DirectControl = new DirectControl();
					m_DirectControl.Size = new System.Drawing.Size(25, 25);
					m_DirectControl.Dock = System.Windows.Forms.DockStyle.Top;
					m_DirectControl.Visible = false;*/

				/*m_ToolBoxDevice = new ToolBoxDevice();
					m_ToolBoxDevice.RenderTarget = m_DirectControl;
					m_ToolBoxDevice.SystemContext = this.SystemContext;

					m_SystemContext.PrimaryWindow.Controls.Add(m_DirectControl);*/

				_ToolBoxHeader = new Cerebrum.Direct3D.Forms.TabHeader();
				_ToolBoxHeader.Direction = Cerebrum.Direct3D.Forms.LayoutDirection.Vertical;
				_ToolBoxHeader.Behavior = Cerebrum.Direct3D.Forms.SelectionBehavior.ManualSelect;
				_ToolBoxHeader.Dock = System.Windows.Forms.DockStyle.Left;
				_ToolBoxHeader.Interval = 2;
				_ToolBoxHeader.SetBounds(0,0,26,26,System.Windows.Forms.BoundsSpecified.Size);
				_ToolBoxHeader.RenderScene += new Cerebrum.Direct3D.Forms.RenderEventHandler(ToolBarDrawHelper.RenderToolBack);

				//m_ToolBoxDevice.Controls.Add(_ToolBoxHeader);

				/*Cerebrum.Direct3D.Forms.TabPage page = new Cerebrum.Direct3D.Forms.TabPage();
					page.SetBounds(0,0,16,16,System.Windows.Forms.BoundsSpecified.Size);
					page.Dock = System.Windows.Forms.DockStyle.Left;
					_ToolBoxHeader.Controls.Add(page);*/

				//m_ToolBoxDevice.Clearing += new EventHandler(m_ToolBoxDevice_Clearing);
				//m_SystemContext.PrimaryWindow.Clearing += new EventHandler(m_ToolBoxDevice.Clear);
				_app.m_ToolBoxHeader = _ToolBoxHeader;

				_app.m_ToolTipClient = new Cerebrum.Direct3D.Forms.ToolTipClient();
			}


			DrawerBandItem item = new DrawerBandItem();
			item.SetBounds(0,0,24,24,System.Windows.Forms.BoundsSpecified.Size);
			item.RenderMode = Cerebrum.Direct3D.Drawing.ImageRenderMode.CenterImage;
			//--item.CommandId = command.CommandId;
			item.MergeOrder = ud.MergeOrder;

			int MergeOrder;
			MergeOrder = item.MergeOrder;

			Cerebrum.Windows.Forms.IUiControl after = null;

			foreach(Cerebrum.Direct3D.Forms.Control ci in _ToolBoxHeader.Controls)
			{
				Cerebrum.Windows.Forms.IUiControl iui = ci as Cerebrum.Windows.Forms.IUiControl;
				if(iui!=null)
				{
					if(iui.MergeOrder > MergeOrder) 
						if(after==null || after.MergeOrder > iui.MergeOrder)
							after = iui;
				}
			}

			_ToolBoxHeader.Controls.Add(item);
			if(after!=null && MergeOrder>=0)
			{
				int i = _ToolBoxHeader.Controls.IndexOf((Cerebrum.Direct3D.Forms.Control)after);
				_ToolBoxHeader.Controls.SetChildIndex((Cerebrum.Direct3D.Forms.Control)item, i);
			}

			_app.m_ToolTipClient.SetMessage(item, ud.DisplayName);
			return item;
		}

		#endregion
	}
}
